1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13  */
14  package com.google.common.collect;
15  
16  import com.google.common.annotations.GwtCompatible;
17  
18  import java.util.NoSuchElementException;
19  import java.util.Set;
20  
21  import javax.annotation.Nullable;
22  
23  /**
24   * An empty contiguous set.
25   *
26   * @author Gregory Kick
27   */
28  @GwtCompatible(emulated = true)
29  @SuppressWarnings("unchecked") // allow ungenerified Comparable types
30  final class EmptyContiguousSet<C extends Comparable> extends ContiguousSet<C> {
31    EmptyContiguousSet(DiscreteDomain<C> domain) {
32      super(domain);
33    }
34  
35    @Override public C first() {
36      throw new NoSuchElementException();
37    }
38  
39    @Override public C last() {
40      throw new NoSuchElementException();
41    }
42  
43    @Override public int size() {
44      return 0;
45    }
46  
47    @Override public ContiguousSet<C> intersection(ContiguousSet<C> other) {
48      return this;
49    }
50  
51    @Override public Range<C> range() {
52      throw new NoSuchElementException();
53    }
54  
55    @Override public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) {
56      throw new NoSuchElementException();
57    }
58  
59    @Override ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) {
60      return this;
61    }
62  
63    @Override ContiguousSet<C> subSetImpl(
64        C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) {
65      return this;
66    }
67  
68    @Override ContiguousSet<C> tailSetImpl(C fromElement, boolean fromInclusive) {
69      return this;
70    }
71  
72    @Override public UnmodifiableIterator<C> iterator() {
73      return Iterators.emptyIterator();
74    }
75  
76    @Override boolean isPartialView() {
77      return false;
78    }
79  
80    @Override public boolean isEmpty() {
81      return true;
82    }
83  
84    @Override public ImmutableList<C> asList() {
85      return ImmutableList.of();
86    }
87  
88    @Override public String toString() {
89      return "[]";
90    }
91  
92    @Override public boolean equals(@Nullable Object object) {
93      if (object instanceof Set) {
94        Set<?> that = (Set<?>) object;
95        return that.isEmpty();
96      }
97      return false;
98    }
99  
100   @Override public int hashCode() {
101     return 0;
102   }
103 }
104